package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_advert_resource;

import cn.com.api.service.ApiU_advert_resourceService;

import cn.com.util.*;
import cn.com.util.*;


public class U_advert_resourceBaseController {

	private static Logger logger = Logger.getLogger(U_advert_resourceBaseController.class);
	
	@Autowired
	private ApiU_advert_resourceService apiU_advert_resourceService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_advert_resource-list")
	public ModelAndView u_advert_resource_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_resource_list.jsp");
		
		List<ApiU_advert_resource> u_advert_resources = apiU_advert_resourceService.findU_advert_resources();
		mv.addObject("u_advert_resources", u_advert_resources);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_advert_resource-add-pre")
	public ModelAndView u_advert_resource_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_resource_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_advert_resource u_advert_resource = apiU_advert_resourceService.findU_advert_resourceById(Integer.valueOf(id));
			mv.addObject("u_advert_resource",u_advert_resource);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_advert_resource-add")
	public void u_advert_resource_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String resource_url = request.getParameter("resource_url");
		String h5_url = request.getParameter("h5_url");
		String sort = request.getParameter("sort");
		String name = request.getParameter("name");
		String advert_id = request.getParameter("advert_id");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_advert_resource u_advert_resource = new ApiU_advert_resource();
            /*
		    u_advert_resource.setId(id);
		    u_advert_resource.setResource_url(resource_url);
		    u_advert_resource.setH5_url(h5_url);
		    u_advert_resource.setSort(sort);
		    u_advert_resource.setName(name);
		    u_advert_resource.setAdvert_id(advert_id);
	        */
			apiU_advert_resourceService.insertU_advert_resource(u_advert_resource);
		}
		//edit
		else{
			ApiU_advert_resource u_advert_resource = apiU_advert_resourceService.findU_advert_resourceById(Integer.valueOf(id));
	        /*
		    u_advert_resource.setId(id);
		    u_advert_resource.setResource_url(resource_url);
		    u_advert_resource.setH5_url(h5_url);
		    u_advert_resource.setSort(sort);
		    u_advert_resource.setName(name);
		    u_advert_resource.setAdvert_id(advert_id);
			*/
			apiU_advert_resourceService.updateU_advert_resource(u_advert_resource);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_advert_resource-delete")
	public void u_advert_resource_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_advert_resourceService.deleteU_advert_resource(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



